<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu mode="inline" :style="{ height: '100%', borderRight: 0 }" @click="handleClick">

        <a-sub-menu v-for="(item) in categoryList" :key="item.id" >
          <template #title>
              <span>
                <user-outlined/>
                {{ item.name }}
              </span>
          </template>
          <template v-if="item.children">
            <a-menu-item v-for="(i) in item.children" :key="i.id">{{ i.name }}</a-menu-item>
          </template>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <a-list item-layout="vertical" size="large" :data-source="books" :grid="{gutter:20, column: 3}">

        <template #renderItem="{ item }">
          <a-list-item key="item.name">
            <template #actions>
          <span v-for="{ type, text } in actions" :key="type">
            <component v-bind:is="type" style="margin-right: 8px"/>
            {{ text }}
          </span>
            </template>

            <a-list-item-meta :description="item.description">
              <template #title>
                <router-link :to="'/doc?ebookId='+item.id">
                  {{ item.name }}
                </router-link>
<!--                <a :href="item.href"></a>-->
              </template>
              <template #avatar>
                <a-avatar :src="item.cover"/>
              </template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
    </a-layout-content>

  </a-layout>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import {UserOutlined, LaptopOutlined, NotificationOutlined} from '@ant-design/icons-vue';
import {categoryAll, ebookList} from '@/api'
import {message} from 'ant-design-vue'
import {Tool} from "@/util/tool";

export default defineComponent({
  name: 'Home',
  components: {
    UserOutlined,
    LaptopOutlined,
    NotificationOutlined,

  },

  setup: function () {
    // 响应变量
    let books = ref()
    const categoryList = ref()
    const pagination = {
      onChange: (page: number) => {
        console.log(page);
      },
      pageSize: 3,
    };
    const actions: Record<string, string>[] = [
      {type: 'StarOutlined', text: '156'},
      {type: 'LikeOutlined', text: '156'},
      {type: 'MessageOutlined', text: '2'},
    ];
    let category2Id = 0
    onMounted(async () => {
      await getList()
      await getCategoryList()
    })
    async function getList(){
      let res = await ebookList({page: 1, size: 100,category2Id:category2Id})
      let data = res.data
      if (res.data.success) {
        books.value = data.content.list
      } else {
        message.error(data.message)
      }
    }
    /*
    * 获取所有分类列表
    * */
    const getCategoryList = async () => {
      let res = await categoryAll()
      categoryList.value = Tool.array2Tree(res.data.content, 0)
    }
    /*
    * 点击侧边栏分类
    *
     */
    const handleClick = async (e:any)=>{
      console.log("点击菜分类",e)
      category2Id = e.key
      await getList()
    }
    return {
      books,
      pagination,
      actions,
      categoryList,
      handleClick
    }
  },
});
</script>
<style scoped>
.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>